Ninja Database Pro
Using Caching
Basic Tasks > Using Caching

By default object caching is enabled for all platforms.  Items are expired after the expiration time or using LRU Least Recently Used when the cache has reached the maximum memory size.  The internal usage date is changed when an item is loaded or saved.  Here are the defaults:

Expiration Time: 20 minutes

 

Maximum Megabytes:

 

public void CacheUsageExample()
{
    int max = 1000;
 
    NinjaDbPro db = new NinjaDbPro("MyDatabaseDirectory", "MyDatabaseName");
    db.OpenDatabase();
 
    //Setting the MaxCacheSize to zero disables the cache
    //By default the MaxCacheSize is 1MB for Windows Phone 7, 2MB for Silverlight, and 5MB for Windows
    db.MaxCacheSize = 0;
 
    //Save some people
    for (int i = 1; i <= max; i++)
    {
        Person person = new Person();
        person.Name = "John " + i;
 
        db.Save(person);
    }
 
    //Load with the cache disabled
    Stopwatch watch = new Stopwatch();
    watch.Start();
    List<Person> persons= db.LoadAll<Person>();
    watch.Stop();
 
    long noCacheTime = watch.ElapsedMilliseconds;
    Console.WriteLine("Elapsed Load Without Cache {0}ms", noCacheTime);
 
    //Set the cache to 5MB
    //By default the MaxCacheSize is 1MB for Windows Phone 7, 2MB for Silverlight, and 5MB for Windows
    db.MaxCacheSize = 5000*1024;
 
    //Set the cache expiration time to 5 minutes
    //The default CacheExpirationTime is 20 minutes for all platforms
    db.CacheExpirationTime = new TimeSpan(0, 0, 5);
    
    //This loads it into the cache
    persons = db.LoadAll<Person>();
 
    //Load from the cache
    watch.Reset();
    watch.Start();
    persons = db.LoadAll<Person>();
    watch.Stop();
 
    long withCacheTime = watch.ElapsedMilliseconds;
    Console.WriteLine("Elapsed Load WITH Cache {0}ms", withCacheTime);
 
    db.CloseDatabase();
}